﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Forms;
using RockCompany.Master.View;

namespace RockCompany.Master.View
{
    public partial class SI07B : ParentFrm
    {
        #region Properties
        private Translator trans;
        private UserAuthenticate uAuth;
        private SI07BFormModel frmModel;
        private string sUserName;
        #endregion

        #region Constructor
        public SI07B()
        {
            InitializeComponent();
            trans = new Translator();
            initDefaultData();
        }

        public SI07B(Translator trans, string sUname, Form caller)
            : base(sUname, caller)
        {
            InitializeComponent();
            sUserName = (string.IsNullOrEmpty(sUname) ? "" : sUname);
            this.trans = trans;
            Owner = caller;
            initDefaultData();            
        } 
        #endregion

        #region Utility Methods
        private void loadMatCheckList()
        {
            string sNo = textBox_ProdImportNo.Text;
            string sfrom = dateTimePicker_FromDate.Value.ToString("yyyyMMdd");
            string sto = dateTimePicker_ToDate.Value.ToString("yyyyMMdd");
            dataGridView_ProdImportList.DataSource = frmModel.searchProdImportList(sNo, sfrom, sto);
        }

        private void initDefaultData()
        {
            frmModel = new SI07BFormModel();
            frmModel.Owner = this;
            trans.controlTranslate(this);
            frmModel.translator = this.trans;

            dateTimePicker_ToDate.Value = DateTime.Today;
            dateTimePicker_FromDate.Value = DateTime.Today.AddDays(-1);

            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);
            PHIEU_NK_TP_CT_DTO temp1 = new PHIEU_NK_TP_CT_DTO();

            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();

            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_PHE_DUYET));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_CREATION_USER));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_CREATION_TIME));
            
            frmModel.buildDataGrid(dataGridView_ProdImportList, properties, null);

            properties.Clear();

            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_CHON));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_MA_MAU));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_SO_LO));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_DON_VI));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_GHI_CHU));

            List<string> readOnlyCols = new List<string>();
            readOnlyCols.Add(PHIEU_NK_TP_CT_DTO.N_MA_MAU);
            readOnlyCols.Add(PHIEU_NK_TP_CT_DTO.N_SO_LO);
            readOnlyCols.Add(PHIEU_NK_TP_CT_DTO.N_MA_MAU);
            readOnlyCols.Add(PHIEU_NK_TP_CT_DTO.N_DON_VI);
            readOnlyCols.Add(PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP);
            readOnlyCols.Add(PHIEU_NK_TP_CT_DTO.N_GHI_CHU);

            frmModel.buildDataGrid(dataGridView_ProdImportDetail, properties, readOnlyCols);
        }
        #endregion

        #region Event Methods
        private void button_SearchProdImport_Click(object sender, EventArgs e)
        {
            loadMatCheckList();
        }

        private void button_DelProdImport_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION),
                                             trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                if (dataGridView_ProdImportList.CurrentRow != null)
                {
                    string msPhieuNK = dataGridView_ProdImportList.CurrentRow.Cells[PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK].EditedFormattedValue.ToString();
                    if (frmModel.removeProdImportDetail(msPhieuNK))
                    {
                        button_SearchProdImport_Click(sender, e);
                        InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.OPERATION_SUCCESS), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                    {
                        InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.OPERATION_FAILURE), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                }
            }
        }

        private void button_ModProdImport_Click(object sender, EventArgs e)
        {
            if (dataGridView_ProdImportList.CurrentRow != null)
            {
                this.Hide();
                if (!CommonModel.existData(frmModel.prodImportDetail))
                {
                    string sNo = dataGridView_ProdImportList.CurrentRow.Cells[PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK].EditedFormattedValue.ToString();
                    dataGridView_ProdImportDetail.DataSource = frmModel.searchProdImportDetail(sNo);
                }
                SI07A newFrm = new SI07A(trans, sUserName, this);
                newFrm.initExistData(frmModel.prodImportDetail);
                newFrm.initDefaultData();
                newFrm.Show();
                button_SearchProdImport_Click(sender, e);
            }
        }

        private void button_NewProdImport_Click(object sender, EventArgs e)
        {
            this.Hide();
            SI07A newFrm = new SI07A(trans, sUserName, this);
            newFrm.initDefaultData();
            newFrm.Show();
        }

        private void button_PrintRP_Click(object sender, EventArgs e)
        {
            if (dataGridView_ProdImportList.CurrentRow != null)
            {
                int index = dataGridView_ProdImportList.CurrentRow.Index;
                string msPhieu = dataGridView_ProdImportList.Rows[index].Cells[PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK].Value.ToString();
                if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_PRINT)))
                {

                    if (frmModel.export2Excel(InforDialog.showSaveFileDlg(this), msPhieu))
                    {
                        InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.SaveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SaveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView_ProdImportList_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridView_ProdImportList.CurrentRow != null)
            {
                string sNo = dataGridView_ProdImportList.CurrentRow.Cells[PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK].EditedFormattedValue.ToString();
                dataGridView_ProdImportDetail.DataSource = frmModel.searchProdImportDetail(sNo);
            }
        }

        private void dataGridView_ProdImportDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView_ProdImportDetail.CurrentCell != null && dataGridView_ProdImportDetail.CurrentCell.ColumnIndex == 0 && e.RowIndex > -1)
                frmModel.addRemoveCheckedList(0, frmModel.getCheckedRow(e.RowIndex));

        }
        #endregion
    }
}
